package com.tengju.motivation.infrastructure.shared;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Optional;

public class DateConvertor {

    public static Date localToDate(LocalDateTime localDateTime) {
        return Optional.ofNullable(localDateTime)
                .map(time -> time.atZone(ZoneId.systemDefault()))
                .map(ZonedDateTime::toInstant)
                .map(Date::from)
                .orElse(null);
    }

    public static LocalDateTime dateToLocal(Date date) {
        return Optional.ofNullable(date)
                .map(Date::toInstant)
                .map(instant -> instant.atZone(ZoneId.systemDefault()))
                .map(ZonedDateTime::toLocalDateTime)
                .orElse(null);
    }
}
